﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using QL_DTTN.DAL;
namespace QL_DTTN.DTO.DTO
{
    public partial class KetQuaThi
    {
        public bool Them(KetQuaThi ketQuaThi)
        {
            string str = " insert into " + _tableName + " (SoBaoDanh,MaMon,KetQua,TenTaiKhoan) values (@pSoBaoDanh,@pMaMon,@pKetQua,@pTenTaiKhoan) ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pSoBaoDanh", ketQuaThi.SoBaoDanh));
            dp.AddParameter(new SqlParameter("@pMaMon", ketQuaThi.MaMon));
            dp.AddParameter(new SqlParameter("@pKetQua", ketQuaThi.KetQua));
            dp.AddParameter(new SqlParameter("@pTenTaiKhoan", ketQuaThi.SoBaoDanh == "" ? Convert.DBNull : ketQuaThi.TenTaiKhoan));
            return dp.RunSQL(str, true);
        }
        public bool Them(List<KetQuaThi> lstKetQuaThi)
        {
            SqlDataProvider dp = new SqlDataProvider();
            List<string> lstStr = new List<string>();
            int i=0;
            foreach (var ketQuaThi in lstKetQuaThi)
            {
                string str = " insert into " + _tableName + " (SoBaoDanh,MaMon,KetQua,TenTaiKhoan) values (@pSoBaoDanh" + i + ",@pMaMon" + i + ",@pKetQua" + i + ",@pTenTaiKhoan" + i + ") ";
                dp.AddParameter(new SqlParameter("@pSoBaoDanh"+i, ketQuaThi.SoBaoDanh));
                dp.AddParameter(new SqlParameter("@pMaMon" + i, ketQuaThi.MaMon));
                dp.AddParameter(new SqlParameter("@pKetQua" + i, ketQuaThi.KetQua));
                dp.AddParameter(new SqlParameter("@pTenTaiKhoan" + i, ketQuaThi.SoBaoDanh == "" ? Convert.DBNull : ketQuaThi.TenTaiKhoan));
                i++;
                lstStr.Add(str);
            }
            return dp.runSQLListQuery(lstStr, true);
        }
        public bool Sua(List<KetQuaThi> lstKetQuaThi)
        {
            SqlDataProvider dp = new SqlDataProvider();
            List<string> lstStr = new List<string>();
            int i = 0;
            foreach (var ketQuaThi in lstKetQuaThi)
            {
                string str = " update " + _tableName + " set KetQua=@pKetQua" + i + ",TenTaiKhoan=@pTenTaiKhoan" + i + " where SoBaoDanh=@pSoBaoDanh" + i + " and MaMon=@pMaMon" + i + " ";
                dp.AddParameter(new SqlParameter("@pSoBaoDanh" + i, ketQuaThi.SoBaoDanh));
                dp.AddParameter(new SqlParameter("@pMaMon" + i, ketQuaThi.MaMon));
                dp.AddParameter(new SqlParameter("@pKetQua" + i, ketQuaThi.KetQua));
                dp.AddParameter(new SqlParameter("@pTenTaiKhoan" + i, ketQuaThi.SoBaoDanh == "" ? Convert.DBNull : ketQuaThi.TenTaiKhoan));
                i++;
                lstStr.Add(str);
            }
            return dp.runSQLListQuery(lstStr, true);
        }
        public bool KiemTraTonTai(KetQuaThi ketQuaThi)
        {
            string str = " Select MaMon from " + _tableName + "  where SoBaoDanh=@pSoBaoDanh and MaMon=@pMaMon ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pSoBaoDanh", ketQuaThi.SoBaoDanh));
            dp.AddParameter(new SqlParameter("@pMaMon", ketQuaThi.MaMon));
            System.Data.SqlClient.SqlDataReader sqlRr = dp.ExDataReader(str, true);
            if (sqlRr != null)
            {
                while (sqlRr.Read())
                {
                    return true;
                }
                dp.CloseConnection();
                return false;
            }
            else
            {
                return false;
            }
        }
        public bool Sua(KetQuaThi ketQuaThi)
        {
            string str = " update " + _tableName + " set KetQua=@pKetQua,TenTaiKhoan=@pTenTaiKhoan where SoBaoDanh=@pSoBaoDanh and MaMon=@pMaMon ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pSoBaoDanh", ketQuaThi.SoBaoDanh));
            dp.AddParameter(new SqlParameter("@pMaMon", ketQuaThi.MaMon));
            dp.AddParameter(new SqlParameter("@pKetQua", ketQuaThi.KetQua));
            dp.AddParameter(new SqlParameter("@pTenTaiKhoan", ketQuaThi.SoBaoDanh=="" ? Convert.DBNull : ketQuaThi.TenTaiKhoan));
            return dp.RunSQL(str, true);
        }
    }
}
